package com.lxw.report.engine.dataModel.report;

import com.alibaba.fastjson.JSON;
import com.lxw.report.engine.dataModel.column.ColumnLinkReportParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单元格
 */
public class ReportDataCell {
    private static  Logger logger = LoggerFactory.getLogger(ReportDataCell.class);

    /**
     * 列信息
     */
    private  ReportColumn column;
    /**
     * 单元格所属列名名称
     */
    private  String name;
    /**
     * 单元格实际值
     */
    private  Object value;
    /**
     * 超链接
     */
    private String hyperlink;

    public ReportDataCell(ReportColumn column, String name, Object value) {
        this.column = column;
        this.name = name;
        this.value = value;
    }

    public ReportColumn getColumn() {
        return column;
    }

    public String getName() {
        return name;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public String getHyperlink() {
        return hyperlink;
    }

    public void setHyperlink(String hyperlink) {
        this.hyperlink = hyperlink;
    }


    public String tranLinkUrl(ReportColumn column, ReportDataRow reportDataRow){
        if(column==null||!column.getBindReported()){
            return null;
        }
        //转换查询参数map
        Long linkReportId=column.getTargetReportId();
        List<ColumnLinkReportParam> columnLinkReportParams = column.getTargetReportParams();
        if(columnLinkReportParams ==null){
            columnLinkReportParams =new ArrayList<>();
        }
        Map<String,String[]> paramMap=new HashMap<>();
        for(ColumnLinkReportParam columnLinkReportParam : columnLinkReportParams){
            //TODO 根据关联参数类型进行判断传值(还需要考虑固定传值情况)
            Object cellValue=null;
            if("dataitem".equalsIgnoreCase(columnLinkReportParam.getParamSource())){
                cellValue= reportDataRow.getCellValue(columnLinkReportParam.getParamName());
            }else if("fixedValue".equalsIgnoreCase(columnLinkReportParam.getParamSource())){
                cellValue= columnLinkReportParam.getParamDefalutValue();
            }
            //如果设置了从维度列获取  但是对应的维度值为null或者 没有对应的维度列  那么使用默认值
            if(cellValue==null){
                paramMap.put(columnLinkReportParam.getParamName(),new String[]{columnLinkReportParam.getParamDefalutValue()});
            }else{
                paramMap.put(columnLinkReportParam.getParamName(),new String[]{cellValue.toString()});
            }
        }
        String urlMapValue="";
        String urlMapStr="&map=";
        if(columnLinkReportParams.size()==0){
            urlMapStr="";
        }else{
            try {
                urlMapValue= URLEncoder.encode(JSON.toJSONString(paramMap), "UTF-8");
                urlMapStr+=urlMapValue;
            } catch (UnsupportedEncodingException e) {
                logger.error("查询参数解码失败: {} 下钻查询参数移除",JSON.toJSONString(paramMap));
                urlMapStr="";
            }
        }
        //TODO 变成实际的下载url
        String LinkUrl=""+"?id="+linkReportId+"&type=published"+urlMapStr;
        return "drillDown('"+LinkUrl+"','"+linkReportId+"','"+urlMapValue+"','new')";
    }

    /**
     * 生成LinkValue
     * @return HTML
     */
    public String getLinkValue() {
        String linkValue="<a onclick=\""+this.hyperlink+"\" >" + this.toString() + "</a>";
        return linkValue;
    }

    @Override
    public String toString() {
        //TODO 重写格式转换功能
//        String displayFormat=this.column.getMetaData().getFormat()==null?"":this.column.getMetaData().getFormat();
//        String formatValue= CellFormatUtil.cellFormat(displayFormat,value);
//        return formatValue;
        return "";
    }
}
